(function (w) {
  const sker = {};
  const run = (data, cb, onFail) => {
    $autox.callHandler("run", `data:${JSON.stringify(data)}`, (d) => {
      try {
        d = d.substring(5);
        const data = JSON.parse(d);
        const { type, payload } = data;
        if (type === "success") {
          cb && cb(payload);
        } else {
          if (onFail) {
            onFail(payload);
          } else {
            weui.toast(payload, {
              duration: 3000,
              className: ``,
            });
          }
        }
      } catch (e) {
        log(`run error:` + e.message);
      }
    });
  };
  const exec = (cmd, data, cb, onFail) => {
    return run({ action: cmd, payload: data }, cb, onFail);
  };
  const log = (data, cb) => {
    if (typeof data === "string") {
      run({ action: "log", payload: data }, cb);
    }
    if (typeof data === "object") {
      run({ action: "log", payload: JSON.stringify(data) }, cb);
    }
    if (Array.isArray(data)) {
      run({ action: "log", payload: JSON.stringify(data) }, cb);
    }
  };
  const login = (mobile, password, cb) => {
    run({ action: "login", payload: { mobile, password } }, cb);
  };

  const navigation = (page, cb) => {
    pageManager.go(page)
    cb && cb();
  };
  sker.exec = exec;
  sker.run = run;
  sker.log = log;
  sker.login = login;
  sker.navigation = navigation;
  w.sker = sker;
})(window);
